
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.xmlbeans.*;
import org.example.comunicationInputSchema.QueryDocument;
import org.example.comunicationInputSchema.QueryDocument.Query;
import org.example.comunicationInputSchema.QueryDocument.Query.Candidates;
import org.example.comunicationInputSchema.QueryDocument.Query.Candidates.Candidate;
import org.example.comunicationInputSchema.QueryDocument.Query.QueryParam;
import org.example.comunicationInputSchema.QueryDocument.Query.Targets;
import org.example.comunicationInputSchema.QueryDocument.Query.Targets.Target;
import org.example.comunicationInputSchema.ReccomendedCinemaDocument;
import org.example.comunicationInputSchema.ReccomendedCinemaDocument.ReccomendedCinema;
import org.example.comunicationInputSchema.ReccomendedCinemaDocument.ReccomendedCinema.Cinema;
import org.example.comunicationInputSchema.ReccomendedCinemaDocument.ReccomendedCinema.Cinema.Film;


public class TestXMLClient {

	
	static String xmlFileOutput="/home/marco/TestXMLBeans/output.xml";
	static String xmlFileInput="/home/marco/TestXMLBeans/input.xml";
	
	/**
	 * @param args
	 * @throws IOException 
	 * @throws XmlException 
	 */
	public static void main(String[] args) throws XmlException, IOException {
		// TODO Auto-generated method stub

		TestXMLClient s=new TestXMLClient();
		 
		s.sendInput();
		
	}	

	void sendInput() throws IOException, XmlException{

		QueryDocument doc = QueryDocument.Factory.newInstance();
		Query root = doc.addNewQuery();
		QueryParam q = root.addNewQueryParam();
		q.setGenre("Comico");
		q.setTitle("Non ci resta che piangere");
		q.setKeywords("pippo,pluto,paperino");
		
		Targets ts = root.addNewTargets();
		Target t = ts.addNewTarget();
		t.setIdfilm(1);
		t.setTitle("pippo");
		t.setStory("ma pippo pippo non lo sa che quando passa......");
		
		Candidates cs = root.addNewCandidates();
		Candidate c = cs.addNewCandidate();
		c.setIdfilm(2);
		c.setTitle("Superman");
		c.setStory("Superman masce sul pianeta kripton e ....");
		org.example.comunicationInputSchema.QueryDocument.Query.Candidates.Candidate.Cinema cin = c.addNewCinema();
		cin.setIdcinema(1);
		cin.setStringValue("Royal");
		
		cin = c.addNewCinema();
		cin.setIdcinema(2);
		cin.setStringValue("Aquila");
		
		
		PostMethod post = new PostMethod("http://localhost:8080/TestXMLBeans/TestXMLServlet");
		post.setRequestEntity(new StringRequestEntity(doc.toString(),null,null));		
		post.setRequestHeader("Content-type", "text/xml; charset=UTF-8");
        		
		HttpClient httpclient = new HttpClient();
        
        // Execute request
        try {
            
    		System.out.println("sending xml");

        	int result = httpclient.executeMethod(post);
            
            // Display status code
            System.out.println("XML Sended with the follow Response status code: " + result);
            
            // Display response
            System.out.println("Response body: ");
            getOutput(post.getResponseBodyAsString());
            
       } finally {
            // Release current connection to the connection pool 
            // once you are done
            post.releaseConnection();
        }	
		System.out.println("xml in input - FINE");
		
	}

	public void getOutput(String xml) throws XmlException, IOException{
		
		  ReccomendedCinemaDocument rcd = ReccomendedCinemaDocument.Factory.parse(xml);		  
		  ReccomendedCinema root=rcd.getReccomendedCinema();
		  
		  Cinema[] cs= root.getCinemaArray();
		  
		  for(Cinema c:cs){ 
			  System.out.println("Cinema : id = "+c.getIdcinema()+" name="+ c.getName() +" Score=" + c.getScore());
			  Film[] fs = c.getFilmArray();
			  for(Film f:fs){
				  System.out.println("Film : id="+ f.getIdfilm() + " name="+f.getStringValue()+" Score=" + f.getScore());
			  }
		  }		
	}
}